/**
 * 判断闰年
 * @param year
 */
export function isLeapYear (year) {
  if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
    return true
  } else {
    return false
  }
}

export function generateYearFromNow (years, beforeNow = 0) {
  const year = new Date().getFullYear()
  return generateYear(year - beforeNow, years)
}

function generateYear (startYear, years) {
  return new Array(years).fill(0).map((e, i) => parseInt(startYear) - i)
}

export function getYearFromFullTime (fullTime) {
  return fullTime ? fullTime.split('-')[0] : null
}

export function getMonthStrFromFullTime (fullTime) {
  return fullTime ? fullTime.split('-')[1] : null
}

export function getMonthFromFullTime (fullTime) {
  return fullTime ? parseInt(fullTime.split('-')[1]) : null
}

export function generateFullTimeByYear (year) {
  return `${year}-01-01 00:00:00`
}

export function generateFullTimeByYearMonth (year, month) {
  month = parseInt(month)
  month = month < 10 ? `0${month}` : month
  return `${year}-${month}-01 00:00:00`
}

export function getMonths () {
  return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}

export const getAge = (borthday) => {
  const year = new Date().getFullYear()
  const borthYear = getYearFromFullTime(borthday)
  return parseInt(year) - parseInt(borthYear)
}

// 解析im时间
export function getDateDiff (dateTimeStamp) {
  let result
  const minute = 1000 * 60
  const hour = minute * 60
  const day = hour * 24
  const month = day * 30
  const now = new Date().getTime()
  const diffValue = now - dateTimeStamp
  if (diffValue < 0) {
    return
  }
  const monthC = diffValue / month
  const weekC = diffValue / (7 * day)
  const dayC = diffValue / day
  const hourC = diffValue / hour
  const minC = diffValue / minute
  if (monthC >= 1) {
    if (monthC <= 12) result = '' + parseInt(monthC) + '月前'
    else {
      result = '' + parseInt(monthC / 12) + '年前'
    }
  } else if (weekC >= 1) {
    result = '' + parseInt(weekC) + '周前'
  } else if (dayC >= 1) {
    result = '' + parseInt(dayC) + '天前'
  } else if (hourC >= 1) {
    result = '' + parseInt(hourC) + '小时前'
  } else if (minC >= 1) {
    result = '' + parseInt(minC) + '分钟前'
  } else {
    result = '刚刚'
  }
  return result
}

// 输入Unix时间戳，返回指定时间格式
export function calcTimeHeader (time) {
  // 格式化传入时间
  const date = new Date(parseInt(time))
  const year = date.getUTCFullYear()
  const month = date.getUTCMonth()
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getUTCMinutes()
  // 获取当前时间
  const currentDate = new Date()
  const currentYear = date.getUTCFullYear()
  const currentMonth = date.getUTCMonth()
  const currentDay = currentDate.getDate()
  // 计算是否是同一天
  if (currentYear == year && currentMonth == month && currentDay == day) {
    // 同一天直接返回
    if (hour > 12) {
      return `下午 ${hour}:${minute < 10 ? '0' + minute : minute}`
    } else {
      return `上午 ${hour}:${minute < 10 ? '0' + minute : minute}`
    }
  }
  // 计算是否是昨天
  const yesterday = new Date(currentDate - 24 * 3600 * 1000)
  if (
    year == yesterday.getUTCFullYear() &&
    month == yesterday.getUTCMonth &&
    day == yesterday.getDate()
  ) {
    // 昨天
    return `昨天 ${hour}:${minute < 10 ? '0' + minute : minute}`
  } else {
    return `${year}-${month + 1}-${day} ${hour}:${minute < 10 ? '0' + minute : minute}`
  }
}

export function formatTime (time) {
  if (time) {
    return time.replace(/-/g, '/')
  } else {
    return null
  }
}

// 输入Unix时间戳，返回指定时间格式
export function todayYestodayTime (time, showHM) {
  // 格式化传入时间
  const date = new Date(parseInt(time))
  const year = date.getUTCFullYear()
  const month = date.getUTCMonth()
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getUTCMinutes()
  // 获取当前时间
  const currentDate = new Date()
  const currentYear = date.getUTCFullYear()
  const currentMonth = date.getUTCMonth()
  const currentDay = currentDate.getDate()
  // 计算是否是同一天
  if (currentYear == year && currentMonth == month && currentDay == day) {
    // 同一天直接返回
    if (hour > 12) {
      return `下午 ${hour}:${minute < 10 ? '0' + minute : minute}`
    } else {
      return `上午 ${hour}:${minute < 10 ? '0' + minute : minute}`
    }
  }
  // 计算是否是昨天
  const yesterday = new Date(currentDate - 24 * 3600 * 1000)
  if (
    year == yesterday.getUTCFullYear() &&
    month == yesterday.getUTCMonth &&
    day == yesterday.getDate()
  ) {
    // 昨天
    return `昨天 ${hour}:${minute < 10 ? '0' + minute : minute}`
  } else {
    return `${month + 1}月${day}日${showHM ? ' ' + hour + ':' + minute : ''}`
  }
}

/**
 * JS获取距当前时间差
 */
export function getTimeDiff (time) {
  var diff = ''
  let symbol = 1
  var timeDiff = time - new Date().getTime()
  symbol = timeDiff < 0 ? -1 : 1
  // 计算相差天数
  var days = Math.ceil(Math.abs(timeDiff) / (24 * 3600 * 1000))
  if (days > 0) {
    diff += days + '天'
  }
  // // 计算相差小时数
  // var leave1 = Math.abs(timeDiff) % (24 * 3600 * 1000)
  // var hours = Math.floor(leave1 / (3600 * 1000))
  // if (hours > 0) {
  //   diff += hours + '小时'
  // } else {
  //   if (diff !== '') {
  //     diff += hours + '小时'
  //   }
  // }

  // // 计算相差分钟数
  // var leave2 = leave1 % (3600 * 1000)
  // var minutes = Math.floor(leave2 / (60 * 1000))
  // if (minutes > 0) {
  //   diff += minutes + '分'
  // } else {
  //   if (diff !== '') {
  //     diff += minutes + '分'
  //   }
  // }
  // // 计算相差秒数
  // var leave3 = leave2 % (60 * 1000)
  // var seconds = Math.round(leave3 / 1000)
  // if (seconds > 0) {
  //   diff += seconds + '秒'
  // } else {
  //   if (diff !== '') {
  //     diff += seconds + '秒'
  //   }
  // }

  return { diff, symbol, isToday: new Date(time).toDateString() === new Date().toDateString() }
}

/**
 * 将大于1000的 单位变为k
 */
export function utilToKilo (num) {
  let returnNum = ''
  num = parseInt(num)
  if (num >= 10000) {
    const frontNum = num % 1000 > 0 ? Math.floor(num / 1000 * 100) / 100 : num / 1000
    returnNum = `${frontNum}K`
  } else {
    returnNum = num
  }
  return returnNum
}
